﻿using System;
using System.Collections.Generic;
using System.Text;

namespace HowtoScene
{

    public class Endian
    {
        public const int BigEndian = 0;
        public const int LittleEndian = 1;
        public static bool IsLittleEndian
        {
            get
            {
                return BitConverter.IsLittleEndian;
            }
        }
        public static void Swap(byte[] bts)
        {
            int half = bts.Length / 2;
            int last = bts.Length - 1;
            for (int i = 0; i <= half; i++)
            {
                byte tmp = bts[i];
                bts[i] = bts[last - i];
                bts[last - i] = tmp;
            }
        }
        public static byte[] Swap2(byte[] bts)
        {
            byte[] ret = new byte[bts.Length];            
            for (int i =0; i <bts.Length ; i++)
            {
                ret[i] = bts[bts.Length - i - 1];
            }
            return ret;
        }
        public static int Swap(int data)
        {
            byte[] bts = BitConverter.GetBytes(data);
            Swap(bts);
            int ret=BitConverter.ToInt32(bts, 0);
            return ret;
        }
        public static Boolean Swap(bool data)
        {
            byte[] bts = BitConverter.GetBytes(data);
            Swap(bts);
            bool ret = BitConverter.ToBoolean(bts, 0);
            return ret;
        }
        public static char Swap(char data)
        {
            byte[] bts = BitConverter.GetBytes(data);
            Swap(bts);
            char ret = BitConverter.ToChar(bts, 0);
            return ret;
        }
        public static long Swap(long data)
        {
            byte[] bts = BitConverter.GetBytes(data);
            Swap(bts);
            long ret = BitConverter.ToInt64(bts, 0);
            return ret;
        }
        public static float Swap(float data)
        {
            byte[] bts = BitConverter.GetBytes(data);
            Swap(bts);
            float ret = BitConverter.ToSingle(bts, 0);
            return ret;
        }
        public static double Swap(double data)
        {
            byte[] bts = BitConverter.GetBytes(data);
            Swap(bts);
            double ret = BitConverter.ToDouble (bts, 0);
            return ret;

        }
    }
}

